package com.chencs.biz.category.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.biz.category.service.CategoryService;
import com.chencs.vo.biz.category.CategoryItemModel;
import com.chencs.vo.biz.category.CategoryModel;
import com.chencs.vo.biz.special.SpecialModel;
import framework.model.ResponseOptUtil;
import framework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by fangzehua on 2017/7/4.
 */
@RequestMapping("/category")
@RestController
public class CategoryController {

  @Autowired
  private CategoryService categoryService;

    @RequestMapping(value = "/doCategoryList", method = RequestMethod.GET)
    public ModelAndView doFavoriteList() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/category/category_list");
        return modelAndView;
    }

    @RequestMapping(value = "/categoryList", method = RequestMethod.GET)
    public String categoryList(){
        String retStr = "";
        try {
            retStr = categoryService.categoryList();
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }


    @RequestMapping(value = "/categoryItemList", method = RequestMethod.POST)
    public String categoryListPost(@RequestBody String cid ){
        String retStr = "";
        try {
            retStr = categoryService.getCategoryItemByCid(cid);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }


    @RequestMapping(value = "/categoryItemList/{cid}", method = RequestMethod.GET)
    public String categoryList(@PathVariable("id") String cid ){
        String retStr = "";
        try {
            retStr = categoryService.getCategoryItemByCid(cid);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }





    /**
     * 跳轉添加分类页面
     * @return
     */
    @RequestMapping(value = "/doCategoryPage", method = RequestMethod.GET)
    public ModelAndView doCategoryPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/category/category_add");
        return modelAndView;
    }


    /**
     * 跳轉添加分类页面
     * @return
     */
    @RequestMapping(value = "/saveCategory", method = RequestMethod.GET)
    public String saveCategory(@RequestBody CategoryModel model) {
        String str = "";

        return str;
    }


    /**
     * 修改分类
     * @return
     */
    @RequestMapping(value = "/updateCategory", method = RequestMethod.POST)
    public String updateCategory(@RequestBody CategoryModel model) {
        String retStr = "";
        try {
            retStr = categoryService.updateCategory(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }



    @RequestMapping(value = "/doEditor/{id}", method = RequestMethod.GET)
    public ModelAndView doEditor(@PathVariable("id") String id , Model model) {
        CategoryModel  mode = null;
        try{
            mode = JSON.parseObject(categoryService.findCategoryById(id),CategoryModel.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        ModelAndView modelAndView = new ModelAndView();
        model.addAttribute("mode",mode);
        modelAndView.setViewName("admin/biz/category/category_editor");
        return modelAndView;
    }



    @RequestMapping(value = "/categoryItem/{id}/{name}", method = RequestMethod.GET)
    public ModelAndView doTaoBaoCategoryList(@PathVariable("id") String id ,@PathVariable("name") String name, Model model) {
        ModelAndView modelAndView = new ModelAndView();
        model.addAttribute("id",id);
        model.addAttribute("name",name);
        modelAndView.setViewName("admin/biz/category/categoryItem_list");
        return modelAndView;
    }

    @RequestMapping(value = "/getCategoryItemByCid/{cid}", method = RequestMethod.GET)
    public String getSpecialProductById(@PathVariable("cid") String cid){
        String retStr = "";
        try {
            retStr = categoryService.getCategoryItemByCid(cid);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }



    @RequestMapping(value = "/doItemEditor/{id}", method = RequestMethod.GET)
    public ModelAndView doEditorCategoryItem(@PathVariable("id") String id,Model model){
        CategoryItemModel mode = null;
        try{
            mode = JSON.parseObject(categoryService.findCategoryItemModelById(id),CategoryItemModel.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        ModelAndView modelAndView = new ModelAndView();
        model.addAttribute("mode",mode);
        modelAndView.setViewName("admin/biz/category/category_item_editor");
        return modelAndView;
    }

    @RequestMapping(value = "/doAddCategoryItem/{cid}", method = RequestMethod.GET)
    public ModelAndView doAddCategoryItem(@PathVariable("cid") String cid,Model model){
        ModelAndView modelAndView = new ModelAndView();
        model.addAttribute("cid",cid);
        modelAndView.setViewName("admin/biz/category/category_item_add");
        return modelAndView;
    }


    @RequestMapping(value = "/insertCategoryItem", method = RequestMethod.POST)
    public String insertCategoryItem(@RequestBody CategoryItemModel model){
        String retStr = "";
        try {
            retStr = categoryService.insertCategoryItem(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }


    @RequestMapping(value = "/updateCategoryItem", method = RequestMethod.POST)
    public String updateCategoryItem(@RequestBody CategoryItemModel model){
        String retStr = "";
        try {
            retStr = categoryService.updateCategoryItem(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }





















    @RequestMapping(value = "/doTaoBaoCategoryList", method = RequestMethod.GET)
    public ModelAndView doTaoBaoCategoryList() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/category/taobaocategory_list");
        return modelAndView;
    }


    @RequestMapping(value = "/taoBaoCategoryList", method = RequestMethod.GET)
    public String getTaoBaoCategoryList(){
        String retStr = "";
        try {
            retStr = categoryService.getTaoBaoCategoryList();
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/delTaoCategory", method = RequestMethod.POST)
    public String delTaoCategory(@RequestBody String id){
        String retStr = "";
        try {
            retStr = categoryService.delTaoCategory(id);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }




}
